package principal;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import ar.uba.fi.algo3.titiritero.vista.*;
import avanzadas.Grabador;

public class Principal extends JFrame implements ObservadorPuntos{
		
		private static final long serialVersionUID = 1L;
		private JPanel jContentPane = null;
		private JToolBar jJToolBarBar = null;
		private JMenuItem jMenuItem = null;
		private JMenuItem jMenuItem1 = null;
		private JMenuItem jMenuGuardar = null;
		private JMenuItem jMenuCargar = null;
		private Panel panel = null;
		
		private AlgoTankMovimiento algoMovimiento;
		private IniciarJuego iniciarJuego = null;
		
		private JPanel jPanelEstado = null;
		private JLabel jLabel = null;
		private JLabel jLabelVidaAlgoTank = null;
		private JLabel jLabelVidaBase = null;
		private JLabel jLabelPuntos = null;
		
		private Grabador grabador;
		
		
		
		private JToolBar getJJToolBarBar() {
			if (jJToolBarBar == null) {
				jJToolBarBar = new JToolBar();
				jJToolBarBar.setBounds(new Rectangle(0, 0, 372, 33));
				jJToolBarBar.add(getJMenuItem());
				jJToolBarBar.add(getJMenuItem1());
				jJToolBarBar.add(getJMenuGrabar());
				jJToolBarBar.add(getJMenuCargar());
				
			}
			return jJToolBarBar;
		}

		
		private JMenuItem getJMenuItem() {
			if (jMenuItem == null) {
				jMenuItem = new JMenuItem();
				jMenuItem.setText("Comenzar");
				jMenuItem.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("actionPerformed()"); 
					}
				});
				jMenuItem.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						comenzar();
					}
				});
			}
			return jMenuItem;
		}
		
		private JMenuItem getJMenuGrabar() {
			if (jMenuGuardar== null) {
				jMenuGuardar = new JMenuItem();
				jMenuGuardar.setText("Guardar");
				jMenuGuardar.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("juegoGrabado()"); // TODO Auto-generated Event stub actionPerformed()
					}
				});
				jMenuGuardar.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						if(grabador != null)
						grabador.grabar();
					}
				});
			}
			return jMenuGuardar;
		}
		
		private JMenuItem getJMenuCargar() {
			if (jMenuCargar == null) {
				jMenuCargar = new JMenuItem();
				jMenuCargar.setText("Cargar");
				jMenuCargar.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("juegoCargado()"); // TODO Auto-generated Event stub actionPerformed()
					}
				});
				jMenuCargar.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						if (grabador != null){
						iniciarJuego.limpiar();	
						grabador.cargar();}
					}
				});
			}
			return jMenuCargar;
		}

		private void comenzar(){
			
			System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
			if (iniciarJuego != null)
				iniciarJuego.limpiar();
			
			else
			iniciarJuego = new 	IniciarJuego(getSuperficieDeDibujo());
			iniciarJuego.setObservadorPuntos(this);
			algoMovimiento.setEscenario(iniciarJuego.getEscenario());
			grabador = new Grabador(iniciarJuego.getEscenario());
			iniciarJuego.comenzarJuego();
			
		}
		
		private void detener() {
			iniciarJuego.detener();		
		}

		
		private JMenuItem getJMenuItem1() {
			if (jMenuItem1 == null) {
				jMenuItem1 = new JMenuItem();
				jMenuItem1.setText("Pausa");
				jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("actionPerformed()"); 
						detener();
					}
				});
			}
			return jMenuItem1;
		}

		
		private Panel getSuperficieDeDibujo() {
			if (panel == null) {
				panel = new Panel(Constantes.ANCHO_PANTALLA , Constantes.ALTO_PANTALLA);
				panel.setLayout(new GridBagLayout());
				panel.setBounds(new Rectangle(0,38 ,Constantes.ANCHO_PANTALLA , Constantes.ALTO_PANTALLA - 33));
			}
			return panel;
		}

		
		private JPanel getJPanelEstado() {
			if (jPanelEstado == null) {
				GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
				gridBagConstraints1.gridx = 2;
				gridBagConstraints1.gridy = 0;
				jLabelPuntos = new JLabel();
				jLabelPuntos.setText("0");
				GridBagConstraints gridBagConstraints = new GridBagConstraints();
				gridBagConstraints.gridx = 0;
				gridBagConstraints.gridy = 0;
				
				GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
				gridBagConstraints2.gridx = 0;
				gridBagConstraints2.gridy = 4;
				GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
				gridBagConstraints3.gridx = 20;
				gridBagConstraints3.gridy = 4;
				
				jLabel = new JLabel();
				jLabel.setText("Puntos: ");
				jLabelVidaAlgoTank = new JLabel();
				jLabelVidaAlgoTank.setText("Vida AlgoTank: ");
				jPanelEstado = new JPanel();
				jLabelVidaBase= new JLabel();
				jLabelVidaBase.setText("Vida Base: ");
				
				jPanelEstado.setLayout(new GridBagLayout());
				jPanelEstado.setBounds(new Rectangle(340, -80, 300, 192));
				jPanelEstado.add(jLabel, gridBagConstraints);
				jPanelEstado.add(jLabelVidaAlgoTank, gridBagConstraints2);
				jPanelEstado.add(jLabelPuntos, gridBagConstraints1);
				jPanelEstado.add(jLabelVidaBase, gridBagConstraints3);
			}
			return jPanelEstado;
		}

		
		private JPanel getJContentPane() {
			if (jContentPane == null) {
				jContentPane = new JPanel();
				jContentPane.setLayout(null);
				jContentPane.add(getJJToolBarBar(), null);
				jContentPane.add(getSuperficieDeDibujo(), null);
				jContentPane.add(getJPanelEstado(), null);
			}
			return jContentPane;
		}

	
		
		public Principal() {
			super();
			initialize();
		}

		
		private void initialize() {
			this.setSize(new Dimension((Constantes.ANCHO_PANTALLA -10 ), (Constantes.ALTO_PANTALLA + 33 )));
			this.setContentPane(getJContentPane());
			this.setTitle("AlgoTank");
						
			algoMovimiento = new AlgoTankMovimiento();
			this.addKeyListener(algoMovimiento);
			
			this.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					System.out.println("windowClosing()"); 
					System.exit(NORMAL);
				}
			});
		}
		
	public static void main(String[] args) {
		
		
		Principal juego = new Principal();
		juego.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		juego.setVisible(true);
	
		
	}


	@Override
	public void seActualizaronLosPuntos(int puntosTotales) {
		this.jLabelPuntos.setText(String.valueOf(puntosTotales));
	}


	@Override
	public void seActualizoVidaBase(int vidaTotal) {
		this.jLabelVidaBase.setText("Vida Base: "+String.valueOf(vidaTotal));
	}


	@Override
	public void seActualizoVidaAlgoTank(int vidaTotal) {
		this.jLabelVidaAlgoTank.setText("Vida AlgoTank: "+String.valueOf(vidaTotal));
	}
	
}
